/*
 *  PIT.c
 *  xD-OS
 *
 *  Created by James Johns on 07/08/2009.
 *  Copyright 2009 xD-Dunce. All rights reserved.
 *
 */

#include "PIT.h"
#include "IDT.h"
#include "textscreen.h"


void initPIT(){

	addServiceRoutine(32, PITHandler);
	ticks = 0;
	screen[(24*80*2)] = '0';
	screen[(24*80*2)+2] = '0';
	screen[(24*80*2)+4] = ':';
	screen[(24*80*2)+6] = '0';
	screen[(24*80*2)+8] = '0';
	screen[(24*80*2)+10] = ':';
	screen[(24*80*2)+12] = '0';
	screen[(24*80*2)+14] = '0';
}

void *PITHandler(){

	ticks++;
	if(ticks%18 == 0){
		seconds++;
		if(seconds >= 60){
			seconds = 0;
			minutes ++;
			if(minutes >= 60){
				minutes = 0;
				hours ++;
				screen[(24*80*2)+0] = (hours/10)+48;
				screen[(24*80*2)+2] = (hours%10)+48;
			}
			screen[(24*80*2)+6] = (minutes/10)+48;
			screen[(24*80*2)+8] = (minutes%10)+48;
		}
		screen[(24*80*2)+12] = (seconds/10)+48;
		screen[(24*80*2)+14] = (seconds%10)+48;
	}
}

void wait(dWord secs){

	dWord stopAt = (ticks + (secs*18));
	while(ticks != stopAt);
}

